import requests;
from _BDTP import bdtp_storage;

'''
BDTP spider
'''
__author__ = 'fuhaotian';

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0"
};

def get_bdtps(keyword, page_count):
    bdtp_images = [];
    base_url_pattern = "https://image.baidu.com/search/acjson?tn=resultjson_com&logid=11204905288092574406" \
                       "&ipn=rj&ct=201326592&is=&fp=result&queryWord=%s&cl=2&lm=-1&ie=utf-8&oe=utf-8" \
                       "&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&word=%s&s=&se=&tab=&width=" \
                       "&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&cg=girl&pn=%d&rn=%d" \
                       "&gsm=b4&1616550957251=";
    page_size = 30;
    url_list = list(base_url_pattern % (keyword, keyword, item * page_size, page_size)
                    for item in range(1, page_count + 1));
    for url in url_list:
        r = requests.get(url, headers=headers);
        if r.status_code == 200:
            images = r.json().get("data")
            for image in images:
                bdtp_images.append(image.get("middleURL"));
        else:
            print("Request error, return status code is: %d" % (r.status_code,));

    print(bdtp_images);
    return bdtp_images;

if __name__ == "__main__":
    keyword = "清华大学";
    images = get_bdtps(keyword, 3);
    bdtp_storage.image_local("/upload", keyword, images);